package cn.panda.edu.teacher;

import cn.panda.edu.model.Video;
import cn.panda.edu.service.VideoService;
import cn.panda.edu.util.FileUtil;
import cn.panda.edu.util.ResultDataUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 类描述：
 */
@RestController
@RequestMapping("/api/teacher/video")
public class TeacherVideoController {

    @Autowired
    private VideoService videoService;


    @ApiOperation(value = "添加视频")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public Object insert(@RequestBody Video video,
                         HttpServletRequest request) {
        int status = 200;
        String message = null;
        try {
            if (video.getVideoId() == null) {
                video.setVideoAddtime(new Date());
                videoService.insertSelective(video);
                message = "添加成功!";
            } else {
                videoService.updateByPrimaryKeySelective(video);
                message = "修改成功!";
            }
            status = 200;
        } catch (Exception e) {
            status = 500;
            message = "添加失败!";
        }
        return ResultDataUtil.resData(status, message, null);
    }

    @ApiOperation(value = "上传视频")
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public Object update(@RequestParam("file") MultipartFile file,
                         HttpServletRequest request) {
        int status = 200;
        String message = null;
        String filePath = null;
        try {
            filePath = FileUtil.uploadFile(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultDataUtil.resData(status, message, filePath);
    }


    @RequestMapping(value = "findOne", method = RequestMethod.POST)
    public Object findOne(@RequestParam("partId") Integer partId,
                          HttpServletRequest request) {
        int status = 200;
        String message = null;
        Video video = null;
        try {
            video = videoService.selectByPart(partId);
            status = 200;
            message = "添加成功!";
        } catch (Exception e) {
            status = 500;
            message = "添加失败!";
        }
        return ResultDataUtil.resData(status, message, video);
    }

}
